﻿namespace ManagerClinic.DanhMuc
{
    using System.Xml;
    using System.Runtime.InteropServices;
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using DevExpress.XtraEditors;
    using ManagerClinic.Core.Common;
    using ManagerClinic.Core.Business;
    using ManagerClinic.Modules;
    using ManagerClinic.Ultilities.Ultility;

    public partial class frmCapNhatDmTonGiao : DevExpress.XtraEditors.XtraForm
    {
        public event EventHandler dmTonGiaoEvent;
        int State = 0;
        ThietLapMaBusiness thietLapMaBusiness = new ThietLapMaBusiness();
        DmTonGiaoBusiness dmTonGiaoBusiness = new DmTonGiaoBusiness();
        DmTonGiaoEntity dmTonGiaoEntity = null;

        public frmCapNhatDmTonGiao(int state)
        {
            InitializeComponent();

            this.State = state;
            txtMaTonGiao.Focus();
        }

        public frmCapNhatDmTonGiao(int state, DmTonGiaoEntity obj)
        {
            InitializeComponent();

            this.State = state;
            dmTonGiaoEntity = obj;

            txtMaTonGiao.Text = dmTonGiaoEntity.MaTonGiao;
            txtTenTonGiao.Text = dmTonGiaoEntity.TenTonGiao;
            txtMoTa.Text = dmTonGiaoEntity.MoTa;
            chkTrangThaiSuDung.Checked = dmTonGiaoEntity.TrangThaiSuDung;
            txtMaTonGiao.Enabled = false;
            txtTenTonGiao.Focus();
        }

        private void frmcapnhatdmtongiao_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.SendWait("{Tab}");
            }
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (validateManager1.IsValid() == false) return;
            if (Validation.ValidSpaceTextBoxKey(txtMaTonGiao, "Mã tôn giáo") == false) return;

            dmTonGiaoEntity = new DmTonGiaoEntity()
            {
                MaTonGiao = txtMaTonGiao.Text,
                TenTonGiao = txtTenTonGiao.Text.Trim(),
                MoTa = txtMoTa.Text.Trim(),
                TrangThaiSuDung = (bool)chkTrangThaiSuDung.Checked
            };

            if (this.State == (int)ConstantManagerClinic.StateInsertOrUpdate.Insert)
            {
                if (dmTonGiaoBusiness.GetDmTonGiao(txtMaTonGiao.Text.Trim()) != null)
                {
                    if (Validation.ValidPrimaryKey(txtMaTonGiao, "Mã tôn giáo") == false) return;
                }
                dmTonGiaoBusiness.InsertDmTonGiao(dmTonGiaoEntity);
                Validation.ClearControl(this);
                txtMaTonGiao.Focus();
            }
            else
            {
                dmTonGiaoBusiness.EditDmTonGiao(dmTonGiaoEntity);
                MessageBox.Show("Cập nhật thành công!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            if (dmTonGiaoEvent != null)
                dmTonGiaoEvent(sender, e);

        }

    }
}